﻿using GalaSoft.MvvmLight;
using Drafter.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using GalaSoft.MvvmLight.Command;

namespace Drafter.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class ChapterViewModel : TreeViewItemViewModel
    {


        #region Ctor
        /// <summary>
        /// Initializes a new instance of the ChapterViewModel class.
        /// </summary>
        public ChapterViewModel(Chapter chapter,ChapterViewModel parent):base(parent)
        {
            InitializeCommands();
            _chapter = chapter;

           // ChildChapters = new ObservableCollection<ChapterViewModel>();
            foreach (var c in _chapter.ChildChapters)
            {
                Children.Add(new ChapterViewModel(c,this));
            }
        }
        
        #endregion

      
        #region Declarations

        Chapter _chapter;

        #endregion //Declarations

        #region Properties

        private Chapter Chapter
        {
            get { return _chapter; }
            set
            {
                _chapter = value;
                RaisePropertyChanged("Chapter");
            }
        }

        
        
  
       

        public String Notes
        {
            get { return _chapter.Notes; }
            set
            {
                _chapter.Notes = value;
                RaisePropertyChanged("Notes");
            }
        }

        public String Text
        {
            get { return _chapter.Text; }
            set
            {
                _chapter.Text = value;
                RaisePropertyChanged("Text");
            }
        }

        public String Title
        {
            get { return _chapter.Title; }
            set
            {
                _chapter.Title = value;
                RaisePropertyChanged("Title");
            }
        }

        #endregion


        #region Commands
        public RelayCommand CreateChildChapterCommand
        {
            get;
            private set;
        }
        public RelayCommand GoUpCommand
        {
            get;
            private set;
        }
        public RelayCommand GoDownCommand
        {
            get;
            private set;
        }

        private void InitializeCommands()
        {
            CreateChildChapterCommand = new RelayCommand(() =>
            {
                 Chapter c = new Chapter() { Title = "New" };


                 this._chapter.ChildChapters.Add(c);
                ChapterViewModel cvm = new ChapterViewModel(c, this);
                this.Children.Add(cvm);
                this.IsExpanded = true;

            });

            GoUpCommand = new RelayCommand(() =>
            {
                int position=this.Parent.Children.IndexOf(this);
                if(position!=0){
                    this.Parent.Children.Move(position,position-1);
                    ((ChapterViewModel)this.Parent).Chapter.ChildChapters.Move(position, position - 1);
                }
            });
            GoDownCommand = new RelayCommand(() =>
            {
                int position = this.Parent.Children.IndexOf(this);
                if (position != this.Parent.Children.Count-1)
                {
                    this.Parent.Children.Move(position, position + 1);
                    ((ChapterViewModel)this.Parent).Chapter.ChildChapters.Move(position, position + 1);
                }
            });
        }

        private ChapterViewModel getRootChapter(ChapterViewModel selectedChapter)
        {
            if (selectedChapter.Parent == null)
            {
                return selectedChapter;
            }
            else
            {
                return getRootChapter(selectedChapter.Parent as ChapterViewModel);
            }
        }
        #endregion



    }
}
 